package io.dbw.modules.utils;

import io.dbw.common.utils.SpringContextUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
/**
 * 国际化封装
 * @author lxc
 * @date 2024/3/31 21:36
 */
@Component
public class MessageUtils
{
    @Resource
    private MessageSource messageSource;

    public String getMessage(String code) {
        return this.getConnectorMessage(code, null, null);
    }

    public String getMessage(String code, String param) {
        return this.getConnectorMessage(code, param, null);
    }

    public String getMessage(String[] codes) {
        return this.getConnectorMessage(null, null, codes);
    }

    private String getConnectorMessage(String code, String param, String[] codes) {
        StringBuilder stringBuilder = new StringBuilder();
        if (StringUtils.isNotEmpty(param)) {
            stringBuilder.append(param).append(":");
        } else if (StringUtils.isNotEmpty(code)) {
            stringBuilder.append(this.messageSource.getMessage(code, null, LocaleContextHolder.getLocale()));
        }
        if (null != codes) {
            for (String arr : codes) {
                stringBuilder.append(this.messageSource.getMessage(arr, null, LocaleContextHolder.getLocale())).append(";");
            }
        }
        return stringBuilder.toString();
    }
}
